import back from "../../../component/goback/index.vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
export default {
  components: {
    VueNeditorWrap: () =>
      import("../../../component/vue-neditor/lib/index.vue"),
    back,
    Editor,
    Toolbar,
  },
  data() {
    return {
      status: true,
      problemid: "",
      text: "",
      title: "",
      content: "",
      solution: "",
      type: "",
      insertdate: "",
      toolbarConfig: {},
      editorConfig: {
        placeholder: "请输入内容...",
        MENU_CONF: {
          uploadImage: {
            server: "/system/v1/common/fileUpload",
            fieldName: "filecontent",
            maxFileSize: 5 * 1024 * 1024, // MaxFileLength: 50, // showBase64:true, // maxNumberOfFiles: 3,
            allowedFileTypes: ["image/jpeg", "image/jpg", "image/png"],
            onSuccess(file, res) {},
            onFailed(file, res) {
              console.log(`${file.name} 上传失败`, res);
              localStorage.setItem("url", res.data.fileurl);
            },
            customInsert(res, insertFn) {
              // 因为自定义插入导致onSuccess与onFailed回调函数不起作用,自己手动处理
              // 先关闭等待的Message
              localStorage.setItem("url", res.data.fileurl);
              console.log(res);
              console.log(this);

              setTimeout(() => {
                insertFn(res.data.fileurl, res.data.fileurl);
              }, 1500);
            },
          },
        },
      },
      editor: {
        customConfig: {
          uploadImgServer: "/system/v1/common/upload",
        },
      },
      mode: "default",
      contents: "", //副文本初始化的内容
      myConfig: {
        // 如果需要上传功能,找后端小伙伴要服务器接口地址
        serverUrl: "/system/v1/common/upload",
        // 你的UEditor资源存放的路径,相对于打包后的index.html
        UEDITOR_HOME_URL: "./assets/NEditor/",
        // 编辑器不自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: 240,
        // 初始容器宽度
        initialFrameWidth: "100%",
        // 关闭自动保存
        enableAutoSave: false,
      },
    };
  },
  activated() {
    this.geteData();
  },
  methods: {
    onChange(data) {
      console.log(this.$refs.editor);
    },
    onChanges(data) {
      console.log(data);
    },
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ，否则会报错
    },
    back() {
      this.$router.back();
    },
    // 获取和设置富文本内容
    ready(editorInstance) {
      // editorInstance.setContent(this.model.content);
      editorInstance.addListener("contentChange", () => {
        this.solution = editorInstance.getContent();
      });
    },
    edit() {
      this.status = false;
    },
    del() {
      let sendData = {
        deleteid: [this.$route.query.problemid],
        type: "0",
      };
      this.$confirm("是否要删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post("/app/v1/platform/problem/delete", sendData)
            .then((res) => {
              if (res.data.code === 1) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.$router.push({
                  path: "/home/feedback",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "已取消删除",
          });
        });
    },
    geteData() {
      this.$axios
        .$post("/app/v1/platform/problem/detail", {
          problemid: this.$route.query.problemid,
        })
        .then((res) => {
          this.title = res.data.data.title;
          this.solution = res.data.data.solution;
          this.type = res.data.data.type;
          this.insertdate = res.data.data.insertdate;
        });
    },
    //提交
    outPut() {
      if (this.title === "") {
        this.$message({
          type: "warning",
          message: "标题不能为空",
        });
        return;
      }
      if (this.solution === "") {
        this.$message({
          type: "warning",
          message: "内容不为空",
        });
        return;
      }
      if (this.type === "") {
        this.$message({
          type: "warning",
          message: "类型不为空",
        });
        return;
      }
      let sendData = {
        problemid: this.$route.query.problemid,
        title: this.title,
        solution: this.solution,
        type: this.type,
      };
      this.$axios
        .$post("/app/v1/platform/problem/updateAndInsert", sendData)
        .then((res) => {
          if (res.data.code === 1) {
            this.$message({
              type: "success",
              message: "提交成功!",
            });
            this.$router.push({
              path: "/home/feedback",
              query: {
                items: 2,
              },
            });
          } else {
            this.$message({
              type: "error",
              message: res.data.message,
            });
          }
        });
    },
    //取消
    cancel() {
      this.$router.back();
      this.$message({
        type: "warning",
        message: "已取消",
      });
    },
  },
};
